<?php
/**
 * Created by PhpStorm.
 * User: MSQ
 * Date: 2021/9/24
 * Time: 14:27
 */

namespace App\Utils;



class RedisHelp
{
    /**
     * @var \EasySwoole\Redis\Redis|null
     */
    protected $redis;

    public function __construct()
    {
        $this->redis = \EasySwoole\RedisPool\RedisPool::defer();
    }

    /**
     * @return \EasySwoole\Redis\Redis|null
     */
    public function getRedis(): ?\EasySwoole\Redis\Redis
    {
        return $this->redis;
    }

    /******* 白友fd ********/
    /******* fd => 绑定信息 ********/
    public function getByFdKey(int $fd): string
    {
        return "hcrm:baiyou:fd:{$fd}";
    }

    public function getByFd(int $fd): array
    {
        $key = $this->getByFdKey($fd);
        $res = $this->redis->hGetAll($key);
        return $res === false ? [] : (array)$res;
    }

    public function setByFd(int $fd, array $data)
    {
        /*if (empty($data)) {
            $data = [
                'project_id' => '',
                'wechatid' => '',
            ];
        }*/

        $key = $this->getByFdKey($fd);
        $this->redis->hMSet($key, $data);
    }

    public function delByFd(int $fd)
    {
        $key = $this->getByFdKey($fd);
        $this->redis->del($key);
    }

    // 白友项目fd集合
    public function getByProjectFdKey(int $projectId): string
    {
        return "hcrm:baiyou:project:{$projectId}";
    }

    public function getByProjectFd(int $projectId): array
    {
        $key = $this->getByProjectFdKey($projectId);
        $res = $this->redis->sMembers($key);
        return $res === false ? [] : (array)$res;
    }

    public function setByProjectFd(int $projectId, int $fd)
    {
        $key = $this->getByProjectFdKey($projectId);
        $this->redis->sAdd($key, $fd);
    }

    public function delByProjectFdRem(int $projectId, int $fd)
    {
        $key = $this->getByProjectFdKey($projectId);
        $this->redis->sRem($key, $fd);
    }

    public function delByProjectFd(int $projectId)
    {
        $key = $this->getByProjectFdKey($projectId);
        $this->redis->del($key);
    }


    /******* 大屏fd ********/
    /******* fd => 绑定信息 ********/
    public function getScreenFdKey(int $fd): string
    {
        return "hcrm:screen:fd:{$fd}";
    }

    public function getScreenFd(int $fd): array
    {
        $key = $this->getScreenFdKey($fd);
        $res = $this->redis->hGetAll($key);
        return $res === false ? [] : (array)$res;
    }

    public function setScreenFd(int $fd, array $data)
    {
        /*if (empty($data)) {
            $data = [
                'project_id' => '',
                'wechatid' => '',
            ];
        }*/

        $key = $this->getScreenFdKey($fd);
        $this->redis->hMSet($key, $data);
    }

    public function delScreenFd(int $fd)
    {
        $key = $this->getScreenFdKey($fd);
        $this->redis->del($key);
    }

    // 白友项目fd集合
    public function getScreenProjectFdKey(int $projectId): string
    {
        return "hcrm:screen:project:{$projectId}";
    }

    public function getScreenProjectFd(int $projectId): array
    {
        $key = $this->getScreenProjectFdKey($projectId);
        $res = $this->redis->sMembers($key);
        return $res === false ? [] : (array)$res;
    }

    public function setScreenProjectFd(int $projectId, int $fd)
    {
        $key = $this->getScreenProjectFdKey($projectId);
        $this->redis->sAdd($key, $fd);
    }

    public function delScreenProjectFdRem(int $projectId, int $fd)
    {
        $key = $this->getScreenProjectFdKey($projectId);
        $this->redis->sRem($key, $fd);
    }

    public function delScreenProjectFd(int $projectId)
    {
        $key = $this->getScreenProjectFdKey($projectId);
        $this->redis->del($key);
    }

    // 百度语音缓存
    public function getBaiduTtsTokenKey(): string
    {
        return "hcrm:baidu:tts";
    }

    public function getBaiduTtsToken()
    {
        $key = $this->getBaiduTtsTokenKey();
        return $this->redis->get($key);
    }

    public function setBaiduTtsToken(string $token, int $expire)
    {
        $key = $this->getBaiduTtsTokenKey();
        $this->redis->set($key, $token, $expire);
    }
}
